﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Model;
using Cashier.supplier;
using Cashier.utils;
using Cashier.goods;
using Cashier.pos;
using Cashier.member;
using Cashier.shipment;
using Cashier.purchase;
using Cashier.rights;
using Cashier.payments;
using Cashier.report;
using Cashier.finance;
using Cashier.data;
using Cashier.help;
using Cashier_Interface;
using Cashier.prints;
using System.Drawing.Printing;
using System.Threading;

namespace Cashier
{
    public partial class MainForm : Form
    {
        private Admin admin;

        /// <summary>
        /// 管理员
        /// </summary>
        public Admin Admin
        {
          get { return admin; }
          set { admin = value; }
        }

        /// <summary>
        /// 根据权限返回查询使用的当前登录用户号
        /// </summary>
        public String UsernameForQuery
        {
            get {
                if (this.admin.Isadmin)
                    return String.Empty;
                else
                    return this.admin.Username;
            }
        }

        /// <summary>
        /// 设置状态栏信息
        /// </summary>
        /// <param name="record"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        public virtual void SetPageInfo(long record, int pageindex, int pagesize){}

        /// <summary>
        /// 添加数据控件
        /// </summary>
        /// <param name="control"></param>
        public virtual void AddDataGridViewPanelControl(Control control){}

        /*
        private LoginForm loginform;
        private Admin admin;
        private static MainForm mainform;
        /// <summary>
        /// 打印参数
        /// </summary>
        private PrintParameter printParameter;
        /// <summary>
        /// 打印设置对象
        /// </summary>
        private PrinterSettings printerSettings;

        /// <summary>
        /// 管理员
        /// </summary>
        public Admin Admin
        {
            get { return admin; }
        }

        /// <summary>
        /// 根据权限返回查询使用的当前登录用户号
        /// </summary>
        public String UsernameForQuery
        {
            get {
                if (this.admin.Isadmin)
                    return String.Empty;
                else
                    return this.admin.Username;
            }
        }
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="loginform"></param>
        private MainForm(LoginForm loginform, Admin admin)
        {
            InitializeComponent();
            this.loginform = loginform;
            this.admin = admin;
            this.printerSettings = printDocument1.PrinterSettings;
            this.pageSetupDialog1.PageSettings.Margins.Left = 40;
            this.pageSetupDialog1.PageSettings.Margins.Right = 40;
            this.pageSetupDialog1.PageSettings.Margins.Top = 60;
            this.pageSetupDialog1.PageSettings.Margins.Bottom = 60;
            this.printParameter = new PrintParameter();

            this.panel_datagridview.Controls.Add(ShipmentListControl.GetShipmentListControl(this));
            //检测新版本
            Thread thread = new Thread(new ThreadStart(CheckVersion));
            thread.Start();
        }

        /// <summary>
        /// 返回MainForm实例
        /// </summary>
        /// <param name="loginform"></param>
        /// <param name="admins"></param>
        /// <returns></returns>
        public static MainForm GetMainForm(LoginForm loginform, Admin admins)
        {
            if (mainform == null)
                mainform = new MainForm(loginform, admins);
            mainform.admin = admins;
            mainform.MenuStripRights();        
            return mainform;
        }

        #region 窗体事件

        /// <summary>
        /// 窗体将要关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要退出软件吗？", "提示", MessageBoxButtons.YesNo);
            if (result == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //关闭登录窗体
            loginform.Close();
        }

        #endregion

        #region 工具栏按钮事件

        /// <summary>
        /// 商品信息查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_goods_query_Click(object sender, EventArgs e)
        {
            GoodsListControl goodsListControl = GoodsListControl.GetGooldsListControl(this);
            this.AddDataGridViewPanelControl(goodsListControl);
        }

        /// <summary>
        /// 新增商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_goods_add_Click(object sender, EventArgs e)
        {
            GoodsAddForm goodsAddForm = new GoodsAddForm(this, GoodsListControl.GetGooldsListControl(this), 0);
            goodsAddForm.ShowDialog();
        }

        /// <summary>
        /// 清点库存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_stock_check_Click(object sender, EventArgs e)
        {
            GoodsInventoryListControl goodsInventroyListControl = GoodsInventoryListControl.GetGoodsInventoryListControl(this);
            this.AddDataGridViewPanelControl(goodsInventroyListControl);
        }

        /// <summary>
        /// 仓库管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_warehouse_query_Click(object sender, EventArgs e)
        {
            WarehouseListControl warehouseListControl = WarehouseListControl.GetWarehouseListControl(this);
            this.AddDataGridViewPanelControl(warehouseListControl);
        }

        /// <summary>
        /// 采购进货
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_purchase_add_Click(object sender, EventArgs e)
        {
            PurchaseAddForm purchaseAddForm = PurchaseAddForm.GetPurchaseAddForm(this, null);
            purchaseAddForm.Show();
        }

        /// <summary>
        /// 采购进货单查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_purchase_query_Click(object sender, EventArgs e)
        {
            PurchaseListControl purchaseListControl = PurchaseListControl.GetPurchaseListControl(this);
            this.AddDataGridViewPanelControl(purchaseListControl);
        }

        /// <summary>
        /// 采购进货商品查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_purchase_goods_query_Click(object sender, EventArgs e)
        {
            PurchaseGoodsListControl purchaseGoodsListControl = PurchaseGoodsListControl.GetPurchaseGoodsListControl(this, String.Empty);
            this.AddDataGridViewPanelControl(purchaseGoodsListControl);
        }

        /// <summary>
        /// 采购商品入库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_purchase_goods_stock_Click(object sender, EventArgs e)
        {
            PurchaseGoodsInStockForm goodsInStockForm = new PurchaseGoodsInStockForm(this, String.Empty);
            goodsInStockForm.ShowDialog();
        }

        /// <summary>
        /// 采购商品入库信息查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_storage_query_Click(object sender, EventArgs e)
        {
            GoodsStorageListControl goodsStorageListControl = GoodsStorageListControl.GetGoodsStorageListControl(this);
            this.AddDataGridViewPanelControl(goodsStorageListControl);
        }

        /// <summary>
        /// 新增采购退货
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_repurchase_add_Click(object sender, EventArgs e)
        {
            RepurchaseAddForm repurchaseAddForm = RepurchaseAddForm.GetRepurchaseAddForm(this, null);
            repurchaseAddForm.Show();
        }

        /// <summary>
        /// 采购退货单查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_repurchase_query_Click(object sender, EventArgs e)
        {
            RepurchaseListControl repurchaseListControl = RepurchaseListControl.GetRepurchaseListControl(this);
            this.AddDataGridViewPanelControl(repurchaseListControl);
        }

        /// <summary>
        /// 采购退货商品查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_repurchase_goods_query_Click(object sender, EventArgs e)
        {
            RepurchaseGoodsListControl repurchaseGoodsListControl = RepurchaseGoodsListControl.GetRepurchaseGoodsListControl(this, null);
            this.AddDataGridViewPanelControl(repurchaseGoodsListControl);
        }

        /// <summary>
        /// POS销售
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_shipment_pos_Click(object sender, EventArgs e)
        {
            if (Config.POS_MODEL.Equals("simple"))
            {
                SimplePosForm simplePosForm = SimplePosForm.GetSimplePosForm(this);
                if (simplePosForm.WindowState == FormWindowState.Minimized)
                    simplePosForm.WindowState = FormWindowState.Maximized;
                else
                    simplePosForm.Show();
            }
            else
            {
                PosForm posform = PosForm.GetPosForm(this);
                if (posform.WindowState == FormWindowState.Minimized)
                    posform.WindowState = FormWindowState.Maximized;
                else
                    posform.Show();
            }
        }

        /// <summary>
        /// 销售出货单查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_shipment_query_Click(object sender, EventArgs e)
        {
            ShipmentListControl shipmentListControl = ShipmentListControl.GetShipmentListControl(this);
            this.AddDataGridViewPanelControl(shipmentListControl);
        }

        /// <summary>
        /// 销售出货商品查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_shipment_goods_query_Click(object sender, EventArgs e)
        {
            ShipmentGoodsListControl shipmentGoodsListControl = ShipmentGoodsListControl.GetShipmentGoodsListControl(this, String.Empty);
            this.AddDataGridViewPanelControl(shipmentGoodsListControl);
        }

        /// <summary>
        /// 新增销售退货
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_reshipment_add_Click(object sender, EventArgs e)
        {
            SalesReturnForm salesReturnForm = SalesReturnForm.GetSalesReturnForm(this, null);
            salesReturnForm.Show();
        }

        /// <summary>
        /// 销售退货查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_reshipment_query_Click(object sender, EventArgs e)
        {
            ReshipmentListControl reshipmentListControl = ReshipmentListControl.GetReshipmentListControl(this);
            this.AddDataGridViewPanelControl(reshipmentListControl);
        }

        /// <summary>
        /// 销售退货商品查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_reshipment_goods_query_Click(object sender, EventArgs e)
        {
            ReshipmentGoodsListControl reshipmentGoodsListControl = ReshipmentGoodsListControl.GetReshipmentGoodsListControl(this, String.Empty);
            this.AddDataGridViewPanelControl(reshipmentGoodsListControl);
        }

        /// <summary>
        /// 销售统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_report_shipment_Click(object sender, EventArgs e)
        {
            ShipmentReportControl shipmentReportControl = ShipmentReportControl.GetShipmentReportControl(this, ReportType.Year);
            this.AddDataGridViewPanelControl(shipmentReportControl);
        }

        /// <summary>
        /// 采购统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_report_purchase_Click(object sender, EventArgs e)
        {
            PurchaseReportControl purchaseReportControl = PurchaseReportControl.GetPurchaseReportControl(this, ReportType.Year);
            this.AddDataGridViewPanelControl(purchaseReportControl);
        }

        /// <summary>
        /// 会员消费排行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_memberTop_Click(object sender, EventArgs e)
        {
            MemberTopReportControl memberTopReportControl = MemberTopReportControl.GetMemberTopReportControl(this);
            this.AddDataGridViewPanelControl(memberTopReportControl);
        }

        /// <summary>
        /// 会员信息查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_member_query_Click(object sender, EventArgs e)
        {
            MemberListControl memberListControl = MemberListControl.GetMemberListControl(this);
            this.AddDataGridViewPanelControl(memberListControl);
        }

        /// <summary>
        /// 新增会员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_member_add_Click(object sender, EventArgs e)
        {
            MemberAddForm memberAddForm = new MemberAddForm(this, null, String.Empty);
            memberAddForm.ShowDialog();
        }

        /// <summary>
        /// 会员积分查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_memberIntegral_query_Click(object sender, EventArgs e)
        {
            MemberIntegralListControl memberIntegralListControl = MemberIntegralListControl.GetMemberIntegralListControl(this);
            this.AddDataGridViewPanelControl(memberIntegralListControl);
        }

        /// <summary>
        /// 会员等级管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_memberGrade_query_Click(object sender, EventArgs e)
        {
            MemberGradeListControl memberGradeListControl = MemberGradeListControl.GetMemberGradeListControl(this);
            this.AddDataGridViewPanelControl(memberGradeListControl);
        }

        /// <summary>
        /// 会员卡充值信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_memberRecharge_query_Click(object sender, EventArgs e)
        {
            MemberRechargeListControl memberRechargeListControl = MemberRechargeListControl.GetMemberRechargeListControl(this, null);
            this.AddDataGridViewPanelControl(memberRechargeListControl);
        }

        /// <summary>
        /// 会员卡消费信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_memberMoney_query_Click(object sender, EventArgs e)
        {
            MemberMoneyListControl memberMoneyListControl = MemberMoneyListControl.GetMemberMoneyListControl(this, null);
            this.AddDataGridViewPanelControl(memberMoneyListControl);
        }

        /// <summary>
        /// 供货商信息查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_supplier_query_Click(object sender, EventArgs e)
        {
            SupplierListControl supplierListControl = SupplierListControl.GetSupplierListControl(this);
            this.AddDataGridViewPanelControl(supplierListControl);
        }

        /// <summary>
        /// 新增供货商信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_supplier_add_Click(object sender, EventArgs e)
        {
            SupplierAddForm supplierAddForm = new SupplierAddForm(this, null, null, 0);
            supplierAddForm.ShowDialog();
        }

        /// <summary>
        /// 员工信息查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_admin_query_Click(object sender, EventArgs e)
        {
            RightsForm rightsform = RightsForm.GetRightsForm(this);
            if (rightsform.WindowState == FormWindowState.Minimized)
                rightsform.WindowState = FormWindowState.Normal;
            else
                rightsform.Show();
        }

        /// <summary>
        /// 会员欠款查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_member_debt_query_Click(object sender, EventArgs e)
        {
            DebtBillShipmentControl debtShipmentControl = DebtBillShipmentControl.GetDebtBillShipmentListControl(this);
            this.AddDataGridViewPanelControl(debtShipmentControl);
        }

        /// <summary>
        /// 会员还款查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_member_repayment_query_Click(object sender, EventArgs e)
        {
            BillShipmentRepaymentControl shipmentRepaymentControl = BillShipmentRepaymentControl.GetBillShipmentRepaymentControl(this, null);
            this.AddDataGridViewPanelControl(shipmentRepaymentControl);
        }

        /// <summary>
        /// 采购欠款查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_purchase_debt_query_Click(object sender, EventArgs e)
        {
            DebtBillPurchaseControl debtBillPurchaseControl = DebtBillPurchaseControl.GetDebtBillPurchaseControl(this);
            this.AddDataGridViewPanelControl(debtBillPurchaseControl);
        }

        /// <summary>
        /// 采购还款查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_purchasePayment_Click(object sender, EventArgs e)
        {
            PurchasePaymentListControl purchasePaymentListControl = PurchasePaymentListControl.GetPurchasePaymentListControl(this, null);
            this.AddDataGridViewPanelControl(purchasePaymentListControl);
        }

        /// <summary>
        /// 其它收支管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_payments_query_Click(object sender, EventArgs e)
        {
            PaymentsControl paymentsControl = PaymentsControl.GetPaymentsControl(this);
            this.AddDataGridViewPanelControl(paymentsControl);
        }

        /// <summary>
        /// 数据备份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_backup_Click(object sender, EventArgs e)
        {
            DataBackupForm dataBackupForm = new DataBackupForm();
            dataBackupForm.ShowDialog();
        }

        /// <summary>
        /// 数据恢复
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_recovery_Click(object sender, EventArgs e)
        {
            DataRecoveryForm dataRecoveryForm = new DataRecoveryForm();
            dataRecoveryForm.ShowDialog();
        }

        /// <summary>
        /// 软件设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_config_Click(object sender, EventArgs e)
        {
            ConfigForm configForm = new ConfigForm();
            configForm.ShowDialog();
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_update_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(System.Windows.Forms.Application.StartupPath + @"\update.exe");
        }

        /// <summary>
        /// 帮助
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_help_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("IExplore.exe", "http://www.daysmei.com/resource/help/index.html");
        }

        /// <summary>
        /// 建议反溃
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_suggest_Click(object sender, EventArgs e)
        {
            SuggestForm suggestForm = new SuggestForm();
            suggestForm.ShowDialog();
        }

        /// <summary>
        /// 关于软件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_about_Click(object sender, EventArgs e)
        {
            AboutForm aboutForm = new AboutForm();
            aboutForm.ShowDialog();
        }

        /// <summary>
        /// 打印所有数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_print_all_Click(object sender, EventArgs e)
        {
            printParameter.IsPrintSelectedRows = false;
            if(this.panel_datagridview.Controls.Count>0)
            {
                Control control = this.panel_datagridview.Controls[0];
                IPrinter iPrinter = control as IPrinter;
                if (iPrinter != null)
                {
                    printParameter.Dgv = iPrinter.GetAllRows();
                    this.PrintPreview();
                }
            }
        }

        /// <summary>
        /// 打印选定数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_print_selected_Click(object sender, EventArgs e)
        {
            printParameter.IsPrintSelectedRows = true;
            if (this.panel_datagridview.Controls.Count > 0)
            {
                Control control = this.panel_datagridview.Controls[0];
                IPrinter iPrinter = control as IPrinter;
                if (iPrinter != null)
                {
                    printParameter.Dgv = iPrinter.GetSelectedRows();
                    this.PrintPreview();
                }
            }
        }

        /// <summary>
        /// 设置打印列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_print_columns_Click(object sender, EventArgs e)
        {
            if (this.panel_datagridview.Controls.Count > 0)
            {
                Control control = this.panel_datagridview.Controls[0];
                IPrinter iPrinter = control as IPrinter;
                if (iPrinter != null)
                {
                    printParameter.Dgv = iPrinter.GetSelectedRows();
                    PrintColumnsForm printColumnsForm = new PrintColumnsForm(this, printParameter.Dgv);
                    printColumnsForm.ShowDialog();
                }
            }
        }

        /// <summary>
        /// 打印机设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_print_setting_Click(object sender, EventArgs e)
        {            
            PrinterSettingForm printerSettingForm = new PrinterSettingForm(this, printerSettings);
            printerSettingForm.ShowDialog();
        }

        /// <summary>
        /// 打印页面设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_print_pageSetting_Click(object sender, EventArgs e)
        {
            this.pageSetupDialog1.ShowDialog();
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_login_out_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            this.loginform.Visible = true;
        }

        /// <summary>
        /// 退出软件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolMenuItem_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region 底部状态工具栏

        public void SetPageInfo(long record, int pageindex, int pagesize)
        {
            if (pagesize > 0)
            {
                StringBuilder sb = new StringBuilder(" 共");
                sb.Append(record).Append("条数据/").Append("每页").Append(pagesize).Append("条  ");
                sb.Append("第").Append(pageindex).Append("页/共").Append(ToolsUtil.GetPagecount(record, pagesize));
                sb.Append("页  ← →左右键快速翻页");
                toolStripStatusLabel_pageinfo.Text = sb.ToString();
            }
            else
                toolStripStatusLabel_pageinfo.Text = String.Empty;
        }

        #endregion

        #region 公共功能方法实现

        /// <summary>
        /// 添加Panel_DataGridView容器控件
        /// </summary>
        /// <param name="control"></param>
        public void AddDataGridViewPanelControl(Control control)
        {
            if (control != null)
            {
                if (!panel_datagridview.Controls.Contains(control))
                {
                    panel_datagridview.Controls.Clear();
                    panel_datagridview.Controls.Add(control);
                    control.Focus();
                }
            }
        }

        /// <summary>
        /// 菜单栏权限
        /// </summary>
        private void MenuStripRights()
        {
            try
            {
                DataTable purviewTable = admin.RolePurviewtable;
                ToolStripItemCollection toolStripItems = this.menuStrip1.Items;
                String rightcode = String.Empty;
                foreach (ToolStripMenuItem item in toolStripItems)
                {
                    ToolStripItemCollection dropDownItems = item.DropDownItems;
                    foreach(ToolStripItem tsItem in dropDownItems)
                    {
                        if (!String.IsNullOrEmpty(tsItem.Text) && tsItem.Tag != null)
                        {
                            rightcode = tsItem.Tag.ToString();
                            DataRow[] rows = purviewTable.Select("code='" + rightcode + "'");
                            if (rows.Length != 1)
                            {
                                if (Config.RIGHTS_BUTTON_STATE.Equals("hidden"))
                                    tsItem.Visible = false;
                                else
                                    tsItem.Enabled = false;
                            }
                        }
                    }
                }

                toolStripItems = this.toolStrip1.Items;
                foreach (ToolStripItem item in toolStripItems)
                {
                    if (!String.IsNullOrEmpty(item.Text) && item.Tag != null)
                    {
                        rightcode = item.Tag.ToString();
                        DataRow[] rows = purviewTable.Select("code='" + rightcode + "'");
                        if (rows.Length != 1)
                        {
                            if (Config.RIGHTS_BUTTON_STATE.Equals("hidden"))
                                item.Visible = false;
                            else
                                item.Enabled = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 打印预览
        /// </summary>
        private void PrintPreview()
        {
            PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
            printPreviewDialog.Document = this.printDocument1;
            printPreviewDialog.PrintPreviewControl.Zoom = 1;
            printPreviewDialog.ShowDialog();
        }

        /// <summary>
        /// 检查程序新版本
        /// </summary>
        private void CheckVersion()
        {
            int upgradeCount = ToolsUtil.CheckNewVersion();
            if (upgradeCount > 0)
            {
                this.toolStripStatusLabel_upgradeinfo.Text = "    软件有新版本更新，请到“帮助“菜单中点击”升级“！";
            }
        }

        #endregion

        #region 权限管理

        /// <summary>
        /// 权限控制
        /// </summary>
        private void PurviewControl()
        {
            DataTable dt = this.admin.RolePurviewtable;
        }

        #endregion

        #region 打印事件
        
        /// <summary>
        /// 打印事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (printParameter.Dgv != null)
            {
                int left = this.pageSetupDialog1.PageSettings.Margins.Left;
                int top = this.pageSetupDialog1.PageSettings.Margins.Top;
                int right = this.pageSetupDialog1.PageSettings.Margins.Right;
                int bottom = this.pageSetupDialog1.PageSettings.Margins.Bottom;
                int print_width = this.pageSetupDialog1.PageSettings.PaperSize.Width - left - right;
                int print_height = this.pageSetupDialog1.PageSettings.PaperSize.Height - top - bottom;
                if (this.pageSetupDialog1.PageSettings.Landscape)
                {
                    left = this.pageSetupDialog1.PageSettings.Margins.Top;
                    top = this.pageSetupDialog1.PageSettings.Margins.Right;
                    right = this.pageSetupDialog1.PageSettings.Margins.Bottom;
                    bottom = this.pageSetupDialog1.PageSettings.Margins.Left;
                    print_width = this.pageSetupDialog1.PageSettings.PaperSize.Height - left - right;
                    print_height = this.pageSetupDialog1.PageSettings.PaperSize.Width - top - bottom;
                }

                printParameter.Left = left;
                printParameter.Top = top;
                printParameter.PrintWidth = print_width;
                printParameter.PrintHeight = print_height;

                printParameter.StartRowIndex = DataGridViewPrint.PrintPages(printParameter, e);
                if (printParameter.StartRowIndex > 0)
                {
                    if (printParameter.StartRowIndex > (printParameter.Dgv.Rows.Count - 1))
                    {
                        printParameter.PageIndex = 1;
                        printParameter.StartRowIndex = 0;
                    }
                    else
                        printParameter.PageIndex += 1;
                }
            }
        }

        /// <summary>
        /// 打印开始前事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

        }

        /// <summary>
        /// 打印结束事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

        }

        private void printDocument1_QueryPageSettings(object sender, System.Drawing.Printing.QueryPageSettingsEventArgs e)
        {

        }

        #endregion
        */
    }
}
